<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <ArticleItem
          v-for="item in list"
          :key="item.art_id"
          :name="item.aut_name"
          :title="item.title"
          :comm="item.comm_count"
          :timer="item.pubdate"
          :cover="item.cover"
          :id="item.art_id"
          @disLike="disFn"
          @reportEV="reportEVFn"
          @click.native="gocle(item.art_id)"
        ></ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from './ArticleItem.vue'
import { getListNewsAPI, doNotLikeAPI, reportAPI } from '@/api/index.js'
import { Notify } from 'vant'
export default {
  name: 'list',
  props: {
    ID: {
      default: '',
      type: Number
    }
  },
  data() {
    return {
      list: [],
      times: new Date().getTime(),
      loading: false,
      finished: false,
      count: 0,
      isLoading: false
    }
  },
  methods: {
    gocle(e) {
      this.$router.push(`/detail?art_id=${e}`)
    },
    async reportEVFn(id, type) {
      try {
        await reportAPI(id, type)
        Notify({ type: 'success', message: '举报成功！' })
      } catch (err) {}
    },
    async disFn(e) {
      try {
        await doNotLikeAPI(e)
        Notify({ type: 'success', message: '反馈成功！' })
      } catch (err) {}
    },
    onRefresh() {
      this.list = []
      ;(this.times = new Date().getTime()), this.getList()
      this.isLoading = false
    },
    onLoad() {
      if (this.list.length === 0) {
        this.loading = false
        return
      }
      this.getList()
    },
    async getList() {
      const { data: resList } = await getListNewsAPI({
        channel_id: this.ID,
        timestamp: this.times
      })
      this.list = [...this.list, ...resList.data.results]
      this.times = resList.data.pre_timestamp
      this.loading = false
      if (resList.data.pre_timestamp === null) {
        this.finished = true
      }
    }
  },
  async created() {
    this.getList()
  },

  components: {
    ArticleItem
  }
}
</script>

<style></style>
